const express = require("express");
const router = express.Router();
const Response = require("../utils/Response");

function getDate() {
  const now = new Date();
  const year = now.getFullYear();
  const month = now.getMonth() + 1;
  const date = now.getDate();
  const hour = ("0" + now.getHours()).slice(-2);
  const minute = ("0" + now.getMinutes()).slice(-2);
  const second = ("0" + now.getSeconds()).slice(-2);
  return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
}

// 引入mysql连接池
const pool = require("../utils/pool");

router.post("/comment-add", (req, res) => {
  let { username, content, avatar, vid } = req.body;
  let date_time = getDate();
  let obj = { username, content, avatar, vid, date_time };
  let sql = "insert into comment set ?";
  pool.query(sql, [obj], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok())
  });
});

module.exports = router;
